package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.Label;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.utils.EventCode;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/6.
 */

public class LabelAdapter extends RecyclerView.Adapter<LabelAdapter.MyViewHolder> {

    private Context context;
    private List<Label> labels;
    public LabelAdapter(Context mContext,List<Label> list){
        context = mContext;
        labels = list;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_label, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (!labels.get(position).isSelected()){
            holder.tvContent.setBackgroundResource(R.drawable.label_bg);
            holder.tvContent.setTextColor(ContextCompat.getColor(context,R.color.label));
        }else {
            holder.tvContent.setBackgroundResource(R.drawable.label_bg_select);
            holder.tvContent.setTextColor(ContextCompat.getColor(context,R.color.white));
        }
        holder.tvContent.setText(labels.get(position).getLabelName());
        holder.tvContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxBus.get().send(EventCode.ON_LABEL_CLICK,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return labels.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_content)
        TextView tvContent;
        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            AutoUtils.autoSize(itemView);
        }
    }
}
